package com.tyk.bookstore.back.message.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.message.model.query.ServiceNoticeAddQuery;
import com.tyk.bookstore.back.message.model.query.ServiceNoticePageQuery;
import com.tyk.bookstore.back.message.model.query.ServiceNoticeUpdateQuery;
import com.tyk.bookstore.back.message.model.vo.ServiceNoticePageVo;
import com.tyk.bookstore.back.message.model.vo.ServiceNoticeVo;
import com.tyk.bookstore.back.message.service.ServiceNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author tyk
 * @description 服务通知管理
 */
@RequestMapping({"api/message/notice/service"})
@Tag(name = "消息管理/服务公告", description = "服务公告管理")
@RestController
@RequiredArgsConstructor
public class ServiceNoticeController {

    private final ServiceNoticeService serviceNoticeService;


    @Operation(summary = "查询服务公告详情", description = "查询服务公告详情")
    @SaCheckPermission("message:notice:service:get")
    @GetMapping({"{id}"})
    public R<ServiceNoticeVo> getById(
            @Schema(description = "公告ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(serviceNoticeService.getById(id));
    }


    @Operation(summary = "分页查询服务公告", description = "分页查询服务公告")
    @SaCheckPermission("message:notice:service:page")
    @GetMapping({"page"})
    public R<ServiceNoticePageVo> getPage(
            @Validated @ParameterObject ServiceNoticePageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(ServiceNoticePageVo.class)) : R.ok(serviceNoticeService.getPage(query));
    }


    @Operation(summary = "添加服务公告", description = "添加服务公告")
    @SaCheckPermission("message:notice:service:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody ServiceNoticeAddQuery query
    ) {
        serviceNoticeService.add(query);
        return R.ok();
    }


    @Operation(summary = "更新服务公告", description = "更新服务公告")
    @SaCheckPermission("message:notice:service:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody ServiceNoticeUpdateQuery query
    ) {
        serviceNoticeService.updateById(query);
        return R.ok();
    }


    @Operation(summary = "删除服务公告", description = "删除服务公告")
    @SaCheckPermission("message:notice:service:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Validated @PathVariable Long id
    ) {
        serviceNoticeService.deleteById(id);
        return R.ok();
    }

}
